package com.example.application.task;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WeatherTask3 implements Runnable{
    private final String TAG="WeatherTask3";
    private Handler handler;
    public Handler getHandler() {
        return handler;
    }
    public void setHandler(Handler handler) {
        this.handler = handler;
    }
    @Override
    public void run() {
        Log.i(TAG, "run:run()....");
        List<String> relist1=new ArrayList<String>();
        List<HashMap<String,String>> relist=new ArrayList<HashMap<String,String>>();
        try {
            //Document doc = Jsoup.connect("https://m.baidu.com/sf?pd=life_compare_weather&openapi=1&dspName=iphone&from_sf=1&resource_id=4495&word=%E5%85%A8%E5%9B%BD%E5%A4%A9%E6%B0%94&title=%E7%9C%81%E5%B8%82%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2&srcid=4983&fromSite=pc").get();
            Document doc1 = Jsoup.connect("https://m.baidu.com/sf?pd=life_compare_weather&openapi=1&dspName=iphone&from_sf=1&resource_id=4495&title=%E7%9C%81%E5%B8%82%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2&srcid=4983&fromSite=pc&t=1654162452949&word=%E5%8D%8E%E5%8C%97%E5%9C%B0%E5%8C%BA").get();
            Document doc2 = Jsoup.connect("https://m.baidu.com/sf?pd=life_compare_weather&openapi=1&dspName=iphone&from_sf=1&resource_id=4495&title=%E7%9C%81%E5%B8%82%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2&srcid=4983&fromSite=pc&t=1654162091989&word=%E4%B8%9C%E5%8C%97%E5%9C%B0%E5%8C%BA").get();
            Document doc3 = Jsoup.connect("https://m.baidu.com/sf?pd=life_compare_weather&openapi=1&dspName=iphone&from_sf=1&resource_id=4495&title=%E7%9C%81%E5%B8%82%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2&srcid=4983&fromSite=pc&t=1654351445636&word=%E5%8D%8E%E4%B8%9C%E5%9C%B0%E5%8C%BA").get();
            Document doc4 = Jsoup.connect("https://m.baidu.com/sf?pd=life_compare_weather&openapi=1&dspName=iphone&from_sf=1&resource_id=4495&title=%E7%9C%81%E5%B8%82%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2&srcid=4983&fromSite=pc&t=1654161784120&word=%E5%8D%8E%E4%B8%AD%E5%9C%B0%E5%8C%BA").get();
            Document doc5 = Jsoup.connect("https://m.baidu.com/sf?pd=life_compare_weather&openapi=1&dspName=iphone&from_sf=1&resource_id=4495&title=%E7%9C%81%E5%B8%82%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2&srcid=4983&fromSite=pc&t=1654161830458&word=%E8%A5%BF%E5%8D%97%E5%9C%B0%E5%8C%BA").get();
            Document doc6 = Jsoup.connect("https://m.baidu.com/sf?pd=life_compare_weather&openapi=1&dspName=iphone&from_sf=1&resource_id=4495&title=%E7%9C%81%E5%B8%82%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2&srcid=4983&fromSite=pc&t=1654161851474&word=%E5%8D%8E%E5%8D%97%E5%9C%B0%E5%8C%BA").get();
            Document doc7 = Jsoup.connect("https://m.baidu.com/sf?pd=life_compare_weather&openapi=1&dspName=iphone&from_sf=1&resource_id=4495&title=%E7%9C%81%E5%B8%82%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2&srcid=4983&fromSite=pc&t=1654674305359&word=%E8%A5%BF%E5%8C%97%E5%9C%B0%E5%8C%BA").get();
//            Elements divs = doc.getElementsByTag("div");
            Elements all1 = doc1.getElementsByClass("sfc-wet-pro-weather-con c-gap-top").first().getElementsByTag("a");
            Elements all2 = doc2.getElementsByClass("sfc-wet-pro-weather-con c-gap-top").first().getElementsByTag("a");
            Elements all3 = doc3.getElementsByClass("sfc-wet-pro-weather-con c-gap-top").first().getElementsByTag("a");
            Elements all4 = doc4.getElementsByClass("sfc-wet-pro-weather-con c-gap-top").first().getElementsByTag("a");
            Elements all5 = doc5.getElementsByClass("sfc-wet-pro-weather-con c-gap-top").first().getElementsByTag("a");
            Elements all6 = doc6.getElementsByClass("sfc-wet-pro-weather-con c-gap-top").first().getElementsByTag("a");
            Elements all7 = doc7.getElementsByClass("sfc-wet-pro-weather-con c-gap-top").first().getElementsByTag("a");
            for(int i=0;i<all1.size();i++){
                Element city=all1.get(i).getElementsByClass("c-span3").first();
                Element temp=all1.get(i).getElementsByClass("c-span3").get(1);
                Element wind=all1.get(i).getElementsByClass("c-span4 sfc-wet-pro-weather-wind").first();
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("city",city.text());
                map.put("temp",temp.text());
                map.put("wind",wind.text());
                relist.add(map);
            }
            for(int i=0;i<all2.size();i++){
                Element city=all2.get(i).getElementsByClass("c-span3").first();
                Element temp=all2.get(i).getElementsByClass("c-span3").get(1);
                Element wind=all2.get(i).getElementsByClass("c-span4 sfc-wet-pro-weather-wind").first();
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("city",city.text());
                map.put("temp",temp.text());
                map.put("wind",wind.text());
                relist.add(map);
            }
            for(int i=0;i<all3.size();i++){
                Element city=all3.get(i).getElementsByClass("c-span3").first();
                Element temp=all3.get(i).getElementsByClass("c-span3").get(1);
                Element wind=all3.get(i).getElementsByClass("c-span4 sfc-wet-pro-weather-wind").first();
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("city",city.text());
                map.put("temp",temp.text());
                map.put("wind",wind.text());
                relist.add(map);
            }
            for(int i=0;i<all4.size();i++){
                Element city=all4.get(i).getElementsByClass("c-span3").first();
                Element temp=all4.get(i).getElementsByClass("c-span3").get(1);
                Element wind=all4.get(i).getElementsByClass("c-span4 sfc-wet-pro-weather-wind").first();
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("city",city.text());
                map.put("temp",temp.text());
                map.put("wind",wind.text());
                relist.add(map);
            }
            for(int i=0;i<all5.size();i++){
                Element city=all5.get(i).getElementsByClass("c-span3").first();
                Element temp=all5.get(i).getElementsByClass("c-span3").get(1);
                Element wind=all5.get(i).getElementsByClass("c-span4 sfc-wet-pro-weather-wind").first();
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("city",city.text());
                map.put("temp",temp.text());
                map.put("wind",wind.text());
                relist.add(map);
            }
            for(int i=0;i<all6.size();i++){
                Element city=all6.get(i).getElementsByClass("c-span3").first();
                Element temp=all6.get(i).getElementsByClass("c-span3").get(1);
                Element wind=all6.get(i).getElementsByClass("c-span4 sfc-wet-pro-weather-wind").first();
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("city",city.text());
                map.put("temp",temp.text());
                map.put("wind",wind.text());
                relist.add(map);
            }
            for(int i=0;i<all7.size();i++) {
                Element city = all7.get(i).getElementsByClass("c-span3").first();
                Element temp = all7.get(i).getElementsByClass("c-span3").get(1);
                Element wind = all7.get(i).getElementsByClass("c-span4 sfc-wet-pro-weather-wind").first();
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("city", city.text());
                map.put("temp", temp.text());
                map.put("wind", wind.text());
                relist.add(map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Message msg = handler.obtainMessage(5); //发送消息
        msg.obj = relist;
        handler.sendMessage(msg);
        Log.i(TAG, "run:sendMessage");
    }
}
